package com.example.module_home.device

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_home.R
import com.example.module_home.databinding.ActivityDeviceAddBinding
import com.example.module_home.entity.AddDeviceEntity
import com.example.module_home.entity.DeviceEntity
import com.example.module_home.entity.DeviceEntityItem
import com.example.module_home.vm.DeviceViewModel
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/DeviceAddActivity")
@AndroidEntryPoint
class DeviceAddActivity : BaseMvvmActivity<ActivityDeviceAddBinding>() {
    private val deviceViewModel : DeviceViewModel by viewModels()

    override fun getLayoutId(): Int =R.layout.activity_device_add

    override fun initViews() {
        binding.devicesTypeSelect.setOnClickListener {
            val typeItem = arrayOf("维修", "投诉", "保洁", "其他")
            val pickerView = OptionsPickerBuilder(this) {postion,_,_,_->
                binding.devicesType.setText(typeItem[postion])
            }.setSubmitText("确定")
                .setCancelText("取消")
                .setTitleText("选择工单类型")
                .build<String>()

            pickerView.setPicker(typeItem.toList())

            pickerView.show()
        }


        val timeItems = mutableListOf("请选择详细地址", "监控设备", "门禁设备", "饮水机")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, timeItems)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.devicesLocation.adapter=adapter
        binding.devicesLocation.setSelection(0)
        binding.devicesLocation.onItemSelectedListener=object :AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long,
            ) {
                if (position==0){
                    //用户选择提示项，可根据需要处理（如不执行操作)
                }else{
                    val selectedItem =timeItems[position]
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                //未选择是处理
            }

        }

        //时间选择器
        binding.devicesTimeSelect.setOnClickListener {
            val datePicker = DatePicker(this)
            datePicker.setTitle("选择日期")
            datePicker.wheelLayout.setRange(
                DateEntity.target(1900,1,1),
                DateEntity.target(2600,12,1)
            )
            datePicker.setOnDatePickedListener { year, month, day ->
                binding.devicesTime.setText("${year}年${month}月${day}日")
            }
            datePicker.show()
        }

        //添加
        binding.commit.setOnClickListener {
            // val deviceName: String,
            //    val deviceType: String,
            //    val failureRate: Double,
            //    val id: Int,
            //    val installationLocation: String,
            //    val status: String
            val entityItem = AddDeviceEntity(
                binding.devicesLocation.selectedItem.toString(),
                binding.devicesType.text.toString(),
                0.2,
                0,
                binding.devicesContent.text.toString(),
                "正常"
            )
            deviceViewModel.AddDevice(entityItem)
        }
        deviceViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
        deviceViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getActivityState(state: Bundle) {

    }

    override fun saveActivityState(outState: Bundle) {
       
    }

}